<template>
  <div class="login-bg">
    <div class="login-center">
      <div class="login-card">
        <div class="login-header">
          <div class="logo">
            <el-icon size="36"><Lock /></el-icon>
          </div>
          <h2 class="login-title">抽奖系统</h2>
          <p class="login-subtitle">欢迎登录</p>
        </div>

        <div class="login-tabs">
          <button :class="{active: tab==='login'}" @click="tab='login'">登录</button>
          <button :class="{active: tab==='register'}" @click="$emit('to-register')">注册</button>
        </div>

        <div class="login-icons">
          <el-tooltip content="微信登录" placement="top">
            <el-icon><i class="fab fa-weixin"></i></el-icon>
          </el-tooltip>
          <el-tooltip content="QQ登录" placement="top">
            <el-icon><i class="fab fa-qq"></i></el-icon>
          </el-tooltip>
          <el-tooltip content="GitHub登录" placement="top">
            <el-icon><i class="fab fa-github"></i></el-icon>
          </el-tooltip>
        </div>

        <el-form :model="form" :rules="rules" ref="loginForm" @keyup.enter="onLogin" @submit.prevent status-icon>
          <el-form-item label="用户名" prop="username">
            <el-input
              v-model="form.username"
              placeholder="请输入用户名"
              clearable
              prefix-icon="User"
              class="custom-input"
            />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="form.password"
              :type="showPwd ? 'text' : 'password'"
              placeholder="请输入密码"
              class="custom-input"
            >
              <template #suffix>
                <el-icon @click="showPwd = !showPwd" class="password-toggle">
                  <View v-if="!showPwd" />
                  <Hide v-else />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="验证码" prop="captcha">
            <div class="captcha-container">
              <el-input
                v-model="form.captcha"
                placeholder="请输入验证码"
                class="captcha-input custom-input"
                @keyup.enter="onLogin"
              />
              <img
                :src="captchaImg"
                @click="getCaptcha"
                alt="验证码"
                class="captcha-image"
              />
            </div>
          </el-form-item>
          <el-form-item>
            <div class="form-options">
              <el-checkbox v-model="form.remember" class="remember-checkbox">记住我</el-checkbox>
              <el-link type="primary" :underline="false" class="forgot-link">忘记密码？</el-link>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="loading" @click="onLogin" class="login-button">
              {{ loading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>
        </el-form>
        <el-alert v-if="msg" :title="msg" :type="msgType" show-icon class="login-alert" />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import request from '../api/request' // 用 request 替换 axios
import { View, Hide, Lock, User } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const emit = defineEmits(['to-register', 'to-lottery', 'to-wins'])

const tab = ref('login')
const form = ref({
  username: '',
  password: '',
  remember: false,
  captcha: '',
  captchaUuid: ''
})
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度为3-20个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度为6-20个字符', trigger: 'blur' }
  ],
  captcha: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { len: 4, message: '验证码为4位字符', trigger: 'blur' }
  ]
}
const loginForm = ref()
const msg = ref('')
const msgType = ref('info')
const loading = ref(false)
const showPwd = ref(false)
const captchaImg = ref('')

const getCaptcha = async () => {
  try {
    const res = await request.get('/captcha')
    captchaImg.value = res.data.img
    form.value.captchaUuid = res.data.uuid
    form.value.captcha = ''
  } catch (e) {
    ElMessage.error('获取验证码失败')
  }
}

onMounted(() => {
  getCaptcha()
  if (localStorage.getItem('username')) {
    form.value.username = localStorage.getItem('username')
    form.value.remember = true
  }
})

const onLogin = async () => {
  await loginForm.value.validate(async valid => {
    if (!valid) return
    loading.value = true
    msg.value = ''
    try {
      const res = await request.post('/user/login', {
        username: form.value.username,
        password: form.value.password,
        captcha: form.value.captcha,
        captchaUuid: form.value.captchaUuid
      })
      msg.value = res.data.message
      msgType.value = res.data.success ? 'success' : 'error'
      if (res.data.success) {
        // 保存 token
        localStorage.setItem('token', res.data.token)
        if (form.value.remember) {
          localStorage.setItem('username', form.value.username)
        } else {
          localStorage.removeItem('username')
        }
        setTimeout(() => {
          emit('to-lottery')
        }, 1000)
      } else {
        getCaptcha()
      }
    } catch (e) {
      msg.value = '登录失败，请检查网络连接'
      msgType.value = 'error'
      getCaptcha()
    } finally {
      loading.value = false
    }
  })
}
</script>

<style scoped>
@import url('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css');

.login-bg {
  min-height: 100vh;
  background: linear-gradient(135deg, #fff5e6 0%, #ffe6cc 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}

.login-center {
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-card {
  background: rgba(255, 255, 255, 0.92);
  border-radius: 20px;
  box-shadow: 0 15px 35px rgba(255, 153, 95, 0.2);
  padding: 40px 35px 30px;
  width: 400px;
  max-width: 95vw;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 184, 108, 0.3);
  position: relative;
  overflow: hidden;
}

.login-card::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255, 184, 108, 0.15) 0%, transparent 70%);
  z-index: -1;
}

.login-header {
  text-align: center;
  margin-bottom: 25px;
}

.logo {
  width: 70px;
  height: 70px;
  background: linear-gradient(135deg, #ff9a3d 0%, #ff6a00 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 15px;
  box-shadow: 0 5px 15px rgba(255, 154, 61, 0.3);
}

.logo .el-icon {
  color: white;
}

.login-title {
  color: #333;
  font-size: 26px;
  font-weight: 600;
  margin: 0 0 5px;
}

.login-subtitle {
  color: #666;
  font-size: 15px;
  margin: 0;
}

.login-tabs {
  display: flex;
  justify-content: center;
  margin-bottom: 25px;
  gap: 12px;
}

.login-tabs button {
  background: rgba(255, 245, 230, 0.8);
  border: none;
  color: #ff9a3d;
  font-size: 16px;
  padding: 10px 25px;
  border-radius: 25px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.login-tabs .active {
  background: linear-gradient(135deg, #ff9a3d 0%, #ff6a00 100%);
  color: white;
  box-shadow: 0 5px 15px rgba(255, 154, 61, 0.4);
}

.login-icons {
  display: flex;
  justify-content: center;
  gap: 25px;
  margin-bottom: 25px;
  font-size: 26px;
}

.login-icons .el-icon {
  background: rgba(255, 245, 230, 0.8);
  border-radius: 50%;
  width: 50px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  color: #ff9a3d;
  box-shadow: 0 4px 10px rgba(255, 154, 61, 0.2);
}

.login-icons .el-icon:hover {
  background: #ff9a3d;
  color: white;
  transform: translateY(-3px);
  box-shadow: 0 6px 15px rgba(255, 154, 61, 0.4);
}

:deep(.el-form-item__label) {
  color: #666;
  font-weight: 500;
}

.custom-input :deep(.el-input__wrapper) {
  background: rgba(255, 245, 230, 0.6);
  border: 1px solid rgba(255, 184, 108, 0.5);
  border-radius: 10px;
  box-shadow: none;
  transition: all 0.3s ease;
}

.custom-input :deep(.el-input__wrapper:hover) {
  border-color: rgba(255, 154, 61, 0.8);
}

.custom-input :deep(.el-input__wrapper.is-focus) {
  border-color: #ff9a3d;
  box-shadow: 0 0 0 2px rgba(255, 154, 61, 0.2);
}

.custom-input :deep(.el-input__inner) {
  color: #333;
  background: transparent;
}

.password-toggle {
  cursor: pointer;
  color: #ff9a3d;
  font-size: 18px;
  transition: color 0.3s;
}

.password-toggle:hover {
  color: #ff6a00;
}

.captcha-container {
  display: flex;
  align-items: center;
  gap: 12px;
}

.captcha-input {
  flex: 1;
}

.captcha-image {
  width: 120px;
  height: 42px;
  border-radius: 8px;
  cursor: pointer;
  border: 1px solid rgba(255, 184, 108, 0.5);
  transition: all 0.3s;
}

.captcha-image:hover {
  border-color: #ff9a3d;
  transform: scale(1.02);
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.remember-checkbox :deep(.el-checkbox__label) {
  color: #666;
}

.forgot-link :deep(.el-link__inner) {
  color: #ff9a3d;
  font-size: 14px;
}

.login-button {
  background: linear-gradient(135deg, #ff9a3d 0%, #ff6a00 100%);
  border: none;
  color: white;
  font-size: 16px;
  font-weight: 600;
  padding: 16px;
  border-radius: 10px;
  width: 100%;
  margin-top: 5px;
  letter-spacing: 1px;
  box-shadow: 0 5px 15px rgba(255, 154, 61, 0.4);
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(255, 154, 61, 0.6);
}

.login-button:active {
  transform: translateY(0);
}

.login-alert {
  margin-top: 20px;
  border-radius: 10px;
  background: rgba(255, 245, 230, 0.6);
  border: 1px solid rgba(255, 184, 108, 0.5);
}

@media (max-width: 480px) {
  .login-card {
    padding: 30px 20px;
  }

  .login-tabs button {
    padding: 8px 20px;
    font-size: 15px;
  }

  .captcha-container {
    flex-direction: column;
    align-items: stretch;
  }

  .captcha-image {
    width: 100%;
    margin-top: 10px;
  }
}
</style>